/*
 * @Description:  网站
 * @Author: Myruun
 * @Date: 2021-04-21 10:15:08
 * @LastEditors: Myruun
 * @LastEditTime: 2021-07-29 10:54:07
 * @FilePath: \serve\controller\resource.js
 */

const mysqlQuery = require('../unit/mysql.js');
const unit = require('../unit/index');


// 获取网站列表
exports.Index = async (ctx, next) => {
	let body = ctx.request.body;
	console.log(unit.getClientIP(ctx.request))
	const id = body.id || '';
	let sql = '';
	if (id) {
		sql =
			"SELECT * FROM resource  WHERE  resource_type ='" +
			id +
			"' order by id desc";
	} else {
		sql = 'SELECT * FROM resource order by id desc';
	}
	let obj = await mysqlQuery(sql);
	ctx.response.body = obj;
};

// 添加网站地址
exports.add = async (ctx, next) => {
	let body = ctx.request.body;
	const title = body.title || '';
	const url = body.url || '';
	const icon = body.icon || '';
	const describe = body.describe || '';
	const resource_type = body.resource_type || '';

	const time = Math.round(new Date() / 1000);
	let sql = `INSERT INTO resource (title, creation_time, updata_time, url, icon,describes,resource_type) VALUES ('${title}','${time}','${time}','${url}','${icon}','${describe}','${resource_type}')`;
	let obj = await mysqlQuery(sql);
	ctx.response.body = { code: 200, data: { msg: '添加成功' } };
};
